package com.ihome.android.market2.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ihome.android.EAApp;
import com.ihome.android.academy.AppMgrActivity;
import com.ihome.android.academy.MainActivity;
import com.ihome.android.http.HttpKeys;
import com.ihome.android.image.CubeImageView;
import com.ihome.android.image.ImageLoader;
import com.ihome.android.academy.R;
import com.ihome.android.market2.asynctask.UninstallAppTask;
import com.ihome.android.market2.frag.InstalledAppFragment.InstalledAppAdapter;
import com.ihome.android.market2.info.AppInfo;
import com.ihome.android.market2.view.DialogFactory.onBtnClickListener;
import com.ihome.android.util.AppUtils;
import com.ihome.android.util.BgInstallApk;
import com.ihome.android.util.Tools;
import com.ihome.android.util.log.LogUtil;

@SuppressLint("NewApi")
public class AppInstalledItem implements OnClickListener {
	private final String		TAG			= AppInstalledItem.class.getSimpleName();
	private RelativeLayout		mRelativeLayout;

	private RelativeLayout		fl_content;
	private FrameLayout			fl_left;

	private CubeImageView		item_icon;
	private TextView			item_appname;
	private TextView			item_sec_info;
	private TextView			item_size;

	//删除按钮
	private TextView			tv_del;
	private FrameLayout			fl_right;

	private Context				context;
	private ImageLoader			imageLoader;

	private InstalledAppAdapter	adapter;
	private AppInfo				appInfo;
	public int					position;

	private boolean				isMove2Del	= false;

	public AppInstalledItem(Context context, ImageLoader imageLoader) {
		this.context = context;
		this.imageLoader = imageLoader;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.app_installed_item, null);
		fl_content = (RelativeLayout) mRelativeLayout.findViewById(R.id.fl_content);
		fl_left = (FrameLayout) mRelativeLayout.findViewById(R.id.fl_left);

		item_icon = (CubeImageView) mRelativeLayout.findViewById(R.id.item_icon);
		item_appname = (TextView) mRelativeLayout.findViewById(R.id.item_appname);
		item_sec_info = (TextView) mRelativeLayout.findViewById(R.id.item_sec_info);
		item_size = (TextView) mRelativeLayout.findViewById(R.id.item_size);

		tv_del = (TextView) mRelativeLayout.findViewById(R.id.tv_del);
		fl_right = (FrameLayout) mRelativeLayout.findViewById(R.id.fl_right);

		fl_content.setOnClickListener(this);
		tv_del.setOnClickListener(this);


		fl_content.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				LogUtil.w(TAG, "onFocusChange() --> fl_content hasFocus = " + hasFocus + "  position = " + position + "  isMove2Del = " + isMove2Del);
				if (hasFocus) {
					fl_left.setVisibility(View.VISIBLE);
					isMove2Del = false;
					adapter.setFocusedPosition(position);
					hasFocus();
				}
				else {
					fl_left.setVisibility(View.GONE);
				}
				checkMainViewFocus();
			}
		});

		tv_del.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				LogUtil.i(TAG, "onFocusChange() --> tv_del hasFocus = " + hasFocus + "  position = " + position);
				if (hasFocus) {
					fl_right.setVisibility(View.VISIBLE);
					hasFocus();
				}
				else {
					fl_right.setVisibility(View.GONE);
					recovered();
				}
				checkMainViewFocus();
			}
		});

	}

	/**
	 * 初始化View，根据AppInfo填充View
	 * 
	 * @param adapter
	 * @param appInfo
	 * @param position
	 */
	public void initView(InstalledAppAdapter adapter, AppInfo appInfo, int position) {
		this.adapter = adapter;
		this.appInfo = appInfo;
		this.position = position;
		if (appInfo != null) {

			if (appInfo != null) {
				//LogUtil.d(TAG, " --> AppInfo = " + appInfo.toString());

				item_appname.setText(appInfo.getAppName());
				if (appInfo.totalsize > 0) {
					item_size.setText(Tools.formateFileSize(context, appInfo.totalsize) + "");
				}

				if (!TextUtils.isEmpty(appInfo.getIconUrl())) {
					item_icon.loadImage(imageLoader, appInfo.getIconUrl());
				}
				else {
					//item_icon.setBackgroundResource(R.drawable.icon_default);
					item_icon.setImageDrawable(appInfo.getIcon());
				}
			}
		}
	}

	private void checkMainViewFocus() {
		if (!mRelativeLayout.hasFocus()) {
			recovered();
		}
	}

	public void recovered() {
		LogUtil.w(TAG, "recovered() -->  position = " + position);
		fl_right.setVisibility(View.GONE);
		fl_left.setVisibility(View.GONE);
		tv_del.setVisibility(View.INVISIBLE);

		mRelativeLayout.setBackgroundResource(R.drawable.list_bg);
		fl_content.setBackgroundResource(R.drawable.list_bg_left_blank);
		tv_del.setBackgroundResource(R.drawable.list_bg_right_blank);
	}

	public void hasFocus() {
		LogUtil.i(TAG, "hasFocus() -->  position = " + position);
		tv_del.setVisibility(View.VISIBLE);
		mRelativeLayout.setBackgroundResource(R.drawable.list_bg_blank);
		fl_content.setBackgroundResource(R.drawable.list_bg_left);
		tv_del.setBackgroundResource(R.drawable.list_bg_right);
	}

	public void requestFocus() {
		fl_left.setVisibility(View.VISIBLE);
		adapter.setFocusedPosition(position);
		hasFocus();
		fl_content.requestFocus();
	}

	public void setClickListener(OnClickListener onClickListener) {
		mRelativeLayout.setOnClickListener(onClickListener);
	}

	public View getView() {
		return mRelativeLayout;
	}

	public void setLayoutParams(RelativeLayout.LayoutParams params) {
		mRelativeLayout.setLayoutParams(params);
	}

	public void setTag(Object tag) {
		mRelativeLayout.setTag(tag);
	}

	public void setOnKeyListener(OnKeyListener onkeylistener) {
		mRelativeLayout.setOnKeyListener(onkeylistener);
		fl_content.setOnKeyListener(onkeylistener);
		tv_del.setOnKeyListener(onkeylistener);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.fl_content:
			LogUtil.w(TAG, "onClick() --> fl_content  position = " + position);
			AppUtils.openApp(context, appInfo.getPkgName());
			
			break;
		case R.id.tv_del:
			LogUtil.v(TAG, "onClick() --> tv_del  position = " + position);
			//BgInstallApk.doUnInstall(context, appInfo.getPkgName());
			new DialogFactory(context).showDialog2Btn(context.getString(R.string.app_name), 
					context.getString(R.string.app_uninstall_tip, appInfo.getAppName()), 
					context.getString(R.string.ok),
					context.getString(R.string.cancel), 
					new onBtnClickListener() {

						@Override
						public void btnLeftClickListener(View v) {
							Bundle b = new Bundle();
							b.putString(HttpKeys.KEY_PACKAGENAME, appInfo.getPkgName());
							if(EAApp.isSystemApp){
								new UninstallAppTask(context).execute(b);
							}else{
								BgInstallApk.doUnInstall(context, appInfo.getPkgName());
							}
							
							if (context instanceof AppMgrActivity) {
								((AppMgrActivity) context).opratePkgName = appInfo.getPkgName();
							}
						}

						@Override
						public void btnNeutralClickListener(View v) {
						}
						@Override
						public void btnRightClickListener(View v) {
						}
						@Override
						public void btnCloseClickListener(View v) {
						}
					}, true);
			
			
			break;
		default:
			break;
		}
	}

}
